package com.rpframework.website.device.act.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Guide;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.GuideService;

/**
 * 服务指南
 * @author Andy_hqr
 * @date 2016年3月8日 下午8:12:45
 */
@Controller
@RequestMapping("api/guide")
public class ApiGuideAct extends AdminAct{
	@Resource GuideService guideservice;
	Gson gson =new Gson();
	/**
	 * 服务指南列表
	 * @param pager
	 * @param model
	 * @param callback
	 * @param session
	 * @return
	 * @time 2016年3月8日 下午8:12:54
	 */
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager", required=false)Pager<Guide> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Guide>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager.getSearchMap().put("propertyid", String.valueOf(sessionuser.getEstateId()));
		pager.getSearchMap().put("typeid", String.valueOf(2));
		pager=guideservice.getpager(pager);
		List<Guide> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Guide act : list) {
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			jsonObj.addProperty("sytime", TagUtils.formatDate(act.getStarttime()));
			array.add(jsonObj);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/**
	 * 服务指南详情根据id查询
	 * @param windesid
	 * @param callback
	 * @param model
	 * @param session
	 * @return
	 * @time 2016年3月8日 下午8:14:40
	 */
	@RequestMapping(value="/listwinedstole",produces = "application/json; charset=utf-8")
	public @ResponseBody String listwineds(@RequestParam String windesid,
		@RequestParam(value = "callback",required = false ) String callback,
			Map<Object, Object> model,
			HttpSession session){
		Guide wind=guideservice.selectOnlyOne(Integer.parseInt(windesid));
		JsonObject json=new JsonObject();
		json.addProperty("sytitle", wind.getTitle());
		json.addProperty("sycontent", wind.getMsg());
		json.addProperty("username", wind.getUsername());
		
		json.addProperty("sytime", TagUtils.formatDate(wind.getStarttime()));
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
}
